<?php
namespace app\admin\controller;

use app\admin\Model\CourseModel;
use app\admin\Model\TeacherModel;
use app\admin\Model\TeachModel;
use cmf\controller\AdminBaseController;
use think\db\Where;
use think\Validate;

class TeachController extends AdminBaseController{
    public function index(){
        extract(input());
        $where = new Where;
        if(isset($tid) && !empty($tid)){
            $where['tid'] = ['like','%'.$tid.'%'];
        }
        if(isset($cid) && !empty($cid)){
            $where['cid'] = ['like','%'.$cid.'%'];
        }
        $date = TeacherModel::all();
        $subject = CourseModel::all();
        $data = TeachModel::with(['teach','course'])->order('id Desc')->where($where)->paginate(3);
        //halt($data);
        $page = $data->render();
        return $this->fetch('',[
            'data' => $data,
            'page' => $page,
            'teacher' => $date,
            'subject' => $subject
        ]);

    }
    public function add() {
       $data = TeacherModel::all();
       $date = CourseModel::all();
       $this->assign('date',$date);
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function addPost() {
        $data = input();
        $rule = [
            'tid' => 'require',
        ];
        $msg = [
            'tid.require' => '老师姓名不能为空',
        ];
        $validate = new Validate($rule, $msg);
        $result = $validate->check($data);
        if (true !== $result) {
            $this->error($validate->getError());
        }
        $add = TeachModel::create($data);
        if ($add){
            return json(['code' => 1 ,'msg' => '添加成功']);
        }else{
            return json (['code' => 2 ,'msg' => '添加失败']);
        }
    }
    public function edit(){
        $id = input('id');
        $teacher = TeacherModel::all();
        $date = CourseModel::all();
        $data = TeachModel::where('id',$id)->find();
        $this->assign('date',$date);
        $this->assign('teacher',$teacher);
        $this->assign('data',$data);
        $this->assign('id',$id);
        return $this->fetch();
    }
    public function editPost(){
        $data = input();
        $rule = [
            'tid'  => 'require',
        ];
        $msg = [
            'tid.require' => '老师姓名不能为空',
        ];
        $validate = new Validate($rule, $msg);
        $result   = $validate->check($data);
        if (true!==$result){
            $this->error($validate->getError());
        }
        $edit = TeachModel::where('id',$data['id'])->update($data);
        if ($edit) {
            return json(['code' => 1 , 'msg' => '修改成功']);
        }else{
            return json(['code' => 2 , 'msg' => '修改失败']);
        }
    }
    public function delete () {
        $id = input('id');
        $del = TeachModel::destroy($id);
        if($del){
            $this->success('删除成功','teach/index');
        }else{
            $this->error('删除失败','teach/index');
        }
    }
    public function delAll() {
        $ids = input('ids/a');
        $del = TeachModel::destroy($ids);
        if($del){
            $this->success('删除成功','teach/index');
        }else{
            $this->error('删除失败','teach/index');
        }
    }
}